import Vue from 'vue'

export default {
  RESET_STATUS (state) {
    Vue.set(state, 'successMsg', '')
    Vue.set(state, 'failedMsg', '')
    Vue.set(state, 'fetchStatus', false)
    Vue.set(state, 'taskComplete', {})
    Vue.set(state, 'drawLottery', {})
    Vue.set(state, 'orderStatus', {})
  },

  SET_SUCCESS_MSG (state, { msg }) {
    Vue.set(state, 'successMsg', msg)
  },

  SET_FAILED_MSG (state, { msg }) {
    Vue.set(state, 'failedMsg', msg)
  },

  SET_TOAST_MSG (state, { msg }) {
    Vue.set(state, 'toastMsg', msg)
  },

  SET_POP_STATUS (state, { type, show }) {
    Vue.set(state, `${type}IsShow`, show)
  },

  // 是否滚动加载
  SET_SCROLL_FETCH_STATUS (state, { status }) {
    Vue.set(state, 'scrollFetch', status)
  },

  SET_APP_DOWN_STATUS (state, { status }) {
    Vue.set(state, 'appDown', status)
  },

  // 礼包列表
  GET_APP_GIFT_LIST (state, { data }) {
    concatList(state, 'appGiftList', data)
  },
  GET_H5_GIFT_LIST (state, { data }) {
    concatList(state, 'h5GiftList', data)
  },

  // 玩家礼包
  SET_USER_GIFT_LIST (state, { data }) {
    data = data || { count: 0, list: [] }
    concatList(state, 'userGiftList', data)
  },

  // 平台消费记录列表
  SET_PTB_CONSUME_LIST (state, { data }) {
    concatList(state, 'ptbConsumeList', data)
  },

  // 首页
  GET_PLAY_INDEX (state, { data }) {
    /* global HuoConfig */
    HuoConfig.SITELOGO = data.site_info.h5_wap_logo
    let siteLogo = document.querySelector('.login_logo')
    siteLogo && siteLogo.setAttribute('src', data.site_info.h5_wap_logo)

    document.title = data.seo.title
    let keywords = document.querySelector('meta[name="keywords"]')
    let description = document.querySelector('meta[name="description"]')
    keywords && keywords.setAttribute('content', data.seo.keyword)
    description && description.setAttribute('content', data.seo.description)

    Vue.set(state, 'playIndex', data)
  },

  // 游戏详情
  GET_GAME_DETAIL (state, { data }) {
    Vue.set(state, 'gameDetail', data)
  },

  // 开服游戏列表
  SET_CURR_SERVER_LIST (state, { data }) {
    concatList(state, 'currServerList', data)
  },
  SET_PREV_SERVER_LIST (state, { data }) {
    concatList(state, 'prevServerList', data)
  },
  SET_NEXT_SERVER_LIST (state, { data }) {
    concatList(state, 'nextServerList', data)
  },

  // 游戏列表
  GET_GAME_LIST (state, { data }) {
    concatList(state, 'gameList', data)
  },

  // 礼包详情
  GET_GIFT_DETAIL (state, { data }) {
    Vue.set(state, 'giftDetail', data)
  },

  // 获取用户信息
  GET_USERINFO (state, { data }) {
    Vue.set(state, 'userinfo', data)
  },

  // 消息列表
  GET_MESSAGE_LIST (state, { data }) {
    concatList(state, 'messageList', data)
  },

  // 常见问题
  GET_SERVICE_INFO (state, { data }) {
    Vue.set(state, 'serviceInfo', data)
  }
}

function concatList (state, name, data) {
  if (state.scrollFetch) {
    let oldData = state[name] || { count: 0, list: [], page: 0 }
    data.list = oldData.list.concat(data.list)
    data.page = oldData.page ? ++oldData.page : 1
  } else {
    data.page = data.page || 1
  }
  Vue.set(state, name, data)
  Vue.set(state, 'fetchStatus', true)
  Vue.set(state, 'scrollFetch', false)
}
